/** 
 * Date:2014年8月7日下午5:22:52 
 * Copyright (c) 2014, YIMAFU Co., Ltd. All right.
 * Author: hujl@weishequan.com
 */
package cn.com.qingz.modules.user.service.impl;

import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import cn.com.qingz.core.constantes.SystemConstantes;
import cn.com.qingz.core.exception.DicmfException;
import cn.com.qingz.modules.user.mapper.UserMapper;
import cn.com.qingz.modules.user.model.Resource;
import cn.com.qingz.modules.user.model.Role;
import cn.com.qingz.modules.user.model.User;
import cn.com.qingz.modules.user.service.UserService;
import cn.com.qingz.modules.user.vo.UserCondition;
import cn.com.qingz.modules.user.vo.UserPrivilegeVo;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    public User login(User user) {
        return userMapper.loginUser(user);
    }

    public List<String> getPermissionByRole(String roleName) {
        String permissions = userMapper.getPermissionByRole(roleName);
        String[] resids = permissions.split(",");

        List<Long> permissionList = new ArrayList<Long>();
        for (String resid : resids) {
            permissionList.add(Long.parseLong(resid));
        }

        List<Resource> resourceList = userMapper.getPermissionByIds(permissionList);

        List<String> res = new ArrayList<String>();
        for (Resource resource : resourceList) {
            res.add(resource.getPermission());
        }
        return res;
    }

    public List<String> getRoleByLoginCode(String logincode) {
        String roleId = userMapper.getRoleByLoginCode(logincode);
        String[] roleids = roleId.split(",");
        List<Long> roleIds = new ArrayList<Long>();

        for (String id : roleids) {
            roleIds.add(Long.parseLong(id));
        }

        List<Role> roles = userMapper.getRoleByIds(roleIds);

        List<String> res = new ArrayList<String>();
        for (Role role : roles) {
            res.add(role.getRole());
        }

        return res;
    }

    public void doRegister(User user) {
        int count = userMapper.countUser(user.getUsername());
        if (count > 0) {
            throw new DicmfException(SystemConstantes.ERROR, "用户名已经存在");
        }
        if (StringUtils.isEmpty(user.getRole_ids())) {
            throw new DicmfException(SystemConstantes.ERROR, "用户名角色不能为空");
        }

        user.setPassword("123456"); //初始化密码
        user.setPassword(new Sha512Hash(user.getPassword()).toBase64());
        user.setCreate_date(new Date());
        user.setOrgId(1L);
        user.setStatus("Y");
        userMapper.register(user);
    }

    public void doUpdateUserInfo(User user) {
        if (StringUtils.isEmpty(user.getRole_ids())) {
            throw new DicmfException(SystemConstantes.ERROR, "用户名角色不能为空");
        }

        User u = userMapper.getUser(user.getId());

        u.setPhoneNumber(user.getPhoneNumber());
        u.setRealName(user.getRealName());
        u.setRole_ids(user.getRole_ids());

        userMapper.doUpdateUserInfo(u);
    }

    public void doUpdatePassword(User user, String newPwd) {
        User u = userMapper.getUser(user.getId());
        String pwdHash = new Sha512Hash(user.getPassword()).toBase64();//之前的密码
        if (!pwdHash.equals(u.getPassword())) {
            throw new DicmfException(SystemConstantes.ERROR, "旧密码错误");
        }
        //设置新密码
        u.setPassword(new Sha512Hash(newPwd).toBase64());
        userMapper.doUpdateUserInfo(u);
    }

    public PageList<UserPrivilegeVo> queryUserPage(UserCondition condition, int page, int limit) {
        return userMapper.queryUserPage(condition, new PageBounds(page, limit));
    }

    public void doSetRole(Long userid, String roleids) {
        User u = userMapper.getUser(userid);
        u.setRole_ids(roleids);
        userMapper.doUpdateUserInfo(u);
    }

    public void doOnOff(Long userid, String status) {
        User u = userMapper.getUser(userid);
        u.setStatus(status);
        userMapper.doUpdateUserInfo(u);
    }

    public void doDisableUsers(String userids) {
        List<String> ids = Arrays.asList(userids.split(","));
        userMapper.updateUserStatus(ids, "N");

    }

    @Override
    public List<Map<String, Object>> test() {
        return userMapper.test();
    }

}
